//
//  HomeBizMenusSmallClassView.m
//  YYZB
//
//  Created by Tongqingqing on 2017/9/27.
//
//

#import "HomeBizMenusSmallClassView.h"
#import "BizMenu.h"
#import <Masonry/Masonry.h>

@interface HomeBizMenusSmallClassView ()<HomeBizMenusCustomViewDelegate>

@property (nonatomic, strong) NSMutableArray *dataModelArr;

@end

@implementation HomeBizMenusSmallClassView

- (instancetype)initWithModel:(NSMutableArray *)modelArray
{
    if (self = [super init]) {
        self.dataModelArr = modelArray;
        [self createView];
    }
    return self;
}

-(void)createView
{
    // 创建一个空view 代表上一个view
    __block HomeBizMenusCustomView *lastView = nil;
    // 间距为10
    int intes = 1;
    // 每行3个
    int num = 1;
    // 循环创建view
    for (int i = 0; i < self.dataModelArr.count; i++) {
        if (i > 1) {
            return;
        }
        
        BizMenu *biz = self.dataModelArr[i];
        
        HomeBizMenusCustomView *view = [[HomeBizMenusCustomView alloc] initWithModel:biz];
        
        //背景图
        if (![biz.FullImgPath isEqual:@""]) {
            view.titleLab.hidden = YES;
        }
        
        view.delegate = self;
        view.tag = i *100;
        [view bindData:self.dataModelArr[i]];
        [self addSubview:view];
        // 添加约束
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            //设置背景图的宽高比！使用下面方法.
            make.height.equalTo(self.mas_height).multipliedBy(0.5f).offset(-intes);
            // 1. 判断是否存在上一个view
            if (lastView) {
                // 存在的话宽度与上一个宽度相同
                make.width.equalTo(lastView);
            } else {
                    make.width.equalTo(self.mas_width).offset(-intes);
            }
            // 2. 判断是否是第一列
            if (i % num == 0) {
                // 一：是第一列时 添加左侧与父视图左侧约束
                make.left.mas_equalTo(view.superview).offset(intes);
            } else {
//                // 二： 不是第一列时 添加左侧与上个view左侧约束
                make.left.mas_equalTo(lastView.mas_right).offset(intes);
            }
            // 3. 判断是否是最后一列 给最后一列添加与父视图右边约束
            if (i % num == (num - 1)) {
                make.right.mas_equalTo(view.superview);
            }
            // 4. 判断是否为第一列
            if (i / num == 0) {
                // 第一列添加顶部约束
                make.top.mas_equalTo(view.superview);
            } else {
                // 其余添加顶部约束 intes*10 是我留出的距顶部高度
                make.top.mas_equalTo(lastView.mas_bottom).offset(intes*2);
                
            }
            
        }];
        // 每次循环结束 此次的View为下次约束的基准
        lastView = view;

        
    }
}

#pragma mark --- HomeBizMenusCustomViewDelegate
- (void)selfClickWithModel:(BizMenu *)model withIndex:(NSInteger)index
{
    if (_delegate &&
        [self.delegate respondsToSelector:@selector(smallClassClickWithModel:withIndex:)]) {
        [_delegate smallClassClickWithModel:model withIndex:index];
    }
}


@end
